package com.binguo.service;

import com.binguo.common.enums.CardType;
import com.binguo.common.enums.PayStatus;
import com.binguo.common.exception.ServiceException;
import com.binguo.helper.FaceHelper;
import com.binguo.model.model.BinguoOrder;
import com.binguo.model.model.PayCard;
import com.binguo.model.repository.BinguoOrderRepository;
import com.binguo.model.repository.PayCardRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;

import java.util.Date;
import java.util.Optional;
import java.util.List;

import static com.binguo.common.exception.ErrorCode.PAYCARD_NOT_SUFFICIENT_FUNDS;
import static com.binguo.common.exception.ErrorCode.WX_ORDER_NOT_EXIST;
import static com.binguo.common.exception.ErrorCode.WX_PAYCARD_NOT_EXIST;


/**
 * Created by haibang on 18/5/7.
 */
@Service
public class PayService {


    @Autowired
    PayCardRepository payCardRepository;

    @Autowired
    FaceHelper faceHelper;

    @Autowired
    OrderService orderService;

    @Autowired
    BinguoOrderRepository binguoOrderRepository;





    public List<PayCard> getPayCardByCustomerId(Integer customerId, CardType cardType){
        Condition condition = new Condition(PayCard.class);
        condition.or().andEqualTo("customerid", customerId)
                .andEqualTo("deleted", false)
        .andEqualTo("cardtype",cardType.getCode().byteValue());
        return payCardRepository.findByCondition(condition);
    }

    public List<PayCard> getPayCardByCustomerId(Integer customerId, byte cardType){
        Condition condition = new Condition(PayCard.class);
        condition.or().andEqualTo("customerid", customerId)
                .andEqualTo("deleted", false)
        .andEqualTo("cardtype",cardType);
        return payCardRepository.findByCondition(condition);
    }

    public List<PayCard> getPayCardByCustomerAndShop(Integer customerId, Integer shopId){
        Condition condition = new Condition(PayCard.class);
        condition.or().andEqualTo("customerid", customerId)
                .andEqualTo("deleted", false)
        .andEqualTo("shopid", shopId);
        return payCardRepository.findByCondition(condition);
    }


    public Optional<PayCard> getPayCard(String cardNo){
        Condition condition = new Condition(PayCard.class);
        condition.or()
                .andEqualTo("cardno", cardNo);
        return payCardRepository.findByCondition(condition).stream().findFirst();
    }

    @Transactional
    public void confirmOrder(BinguoOrder order, PayCard payCard){
        order.setPaytime(new Date());
        order.setPaystatus(PayStatus.SUCCESS.getCode().byteValue());
        binguoOrderRepository.update(order);
        Long balance = payCard.getAccountbalance();
        Long newBalance = balance - order.getRealfee();
        if(newBalance < 0 ){ throw new ServiceException(PAYCARD_NOT_SUFFICIENT_FUNDS);}
        payCard.setAccountbalance(newBalance);
        payCardRepository.update(payCard);
    }



}
